package com.itext.utils;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.JFreeChart;

import com.itextpdf.awt.FontMapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * iText工具类
 * 
 * @author mbc
 * 2015/10/22
 */
public class ItextUtils {

	/**
	 * 新建一个Pdf文档
	 * @param title 文档标题
	 * @param author  文档作者
	 * @param subject 文档主题
	 */
	public static Document createNewPdf(String title,String author,String subject){
		Document doc = new Document();
		//添加标题
		doc.addTitle(title);
		//添加作者
		doc.addAuthor(author);
		//添加主题
		doc.addSubject(subject);
		return doc;
	}
	
	/**
	 * 添加标题
	 * @param document 新建的文档对象
	 * @param cb
	 * @param text  标题内容
	 * @param fontSize 字体大小
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void addHeader(Document document,String text,float fontSize) throws DocumentException, IOException{
		Font font = new Font(CustomFont.getLxksfFont(),fontSize,Font.BOLD);
		Paragraph header = new Paragraph(text,font);
		document.add(header);
		//ColumnText.showTextAligned(doc,Element.ALIGN_CENTER,header,(document.right() - document.left()) / 2 + document.leftMargin(),document.top() + 10, 0);
	}
	
	
	public static void addParagraph(){
		
	}
	
	/**
	 *  画图
	 * @param cb
	 * @param chart  JFreeChart对象
	 * @param width  图表宽度
	 * @param height 图表高度
	 * @param mapper 
	 * @return
	 * @throws IOException 
	 * @throws DocumentException 
	 */
	public static PdfTemplate drawChart(PdfContentByte cb,JFreeChart chart,float width, float height, FontMapper mapper) throws DocumentException, IOException{
		
		PdfTemplate tp = cb.createTemplate(width, height);
		tp.setFontAndSize(CustomFont.getDefaultFont(), 12);
		Graphics2D g2 = tp.createGraphics(width, height,mapper);
		
		Rectangle2D r2D = new Rectangle2D.Double(0,0,width,height);
		
		chart.draw(g2, r2D);
		g2.dispose();
		return tp;
	}
	
	public static void EncryptPdf(String filePath) throws DocumentException, IOException{
		PdfReader reader = new PdfReader(filePath);
		String dest = "temp.pdf";
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
		stamper.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
		stamper.close();
		reader.close();
		File sfile = new File(filePath);
		File nfile = new File(dest);
		//删除原文件
		if(sfile.isFile() && sfile.exists()){
			sfile.delete();
		}
		//重命名新文件为原文件
		if(nfile.isFile() && nfile.exists()){
			nfile.renameTo(new File(filePath));
		}
	}

}
